<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>防抖函数</title>
</head>
<body>
  <div>
    防抖
  </div>
  <input id="aa" type="text">
  <script>
    const input = document.getElementById('aa')
    // 当用户输入完毕的时候，才发送一次网络请求
    function debounce(fn, delay) {
      let timer = null;
      return function () {
        if(timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          fn()
        }, delay)
      }
    }
    input.addEventListener('input', debounce(() => {
      console.log('发送网络请求')
    }, 1000))
  </script>
</body>
</html>